php程序员业余承接网页制作、软件开发。

有需要请联系13714715608

看看案例...

StrutsSpringTestCase work well

2014-01-09T09:08:00.000000Z yang

package com.wudimei.user;

import com.opensymphony.xwork2.ActionProxy;
import org.apache.struts2.StrutsSpringTestCase;
import org.junit.Test;

/**
 *
 * @author rong
 */
public class UserAction2JUnitTest extends StrutsSpringTestCase {

    public UserAction2JUnitTest() {
    }

    @Test
    public void testRegister() throws Exception {

        ActionProxy proxy = null;
        UserAction act = null;

        request.setParameter("user.username", "ddd");
        request.setParameter("user.password", "333");
        request.setParameter("user.email", "yaqy@qq.com");
        request.setParameter("user.gender", "1");
        request.setParameter("user.realname", "杨庆荣");
        request.setParameter("password2", "");

        proxy = getActionProxy("/user/perform_register.do");
        act = (UserAction) proxy.getAction();

        String result = proxy.execute();
        System.out.println(result);
         
        assertEquals("register", result);

    }
}

it's easier than my own way:

 

 

package com.wudimei.user;


import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class UserActionJUnitTest {
    
    public UserActionJUnitTest() {
    }
    
    @BeforeClass
    public static void setUpClass() {
    }
    
    @AfterClass
    public static void tearDownClass() {
    }
    
    @Before
    public void setUp() {
    }
    
    @After
    public void tearDown() {
    }

    @Test
    public void testPerform_register() throws Exception {
        
        BeanFactory beanFactory;
        beanFactory = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        UserDao userDao;
        userDao = (UserDao)beanFactory.getBean("userDao");
               
        UserService userService = (UserService) beanFactory.getBean("userService");
        userService.setUserDao(userDao);
        
        UserAction act;
        act = new UserAction();
        act.setUserService(userService);
       
        
        UserTable user;
        user = new UserTable();
        user.setUsername("yaqy12345");
        user.setPassword("123456");
        user.setEmail("yaqy2qq.com");
        user.setGender(1);
        user.setRealname("yang");
        act.setUser(user);
        
        act.setPassword2("123456");
        
        String res = act.perform_register();
        
        System.out.println( "res:" + res + " ,msg:" +act.getMsg() );
        assertTrue( res.equals("register") );
    }

    
}